//
//  ZXZBaseViewController.swift
//  up_learnWB
//
//  Created by Mac on 16/10/20.
//  Copyright © 2016年 www.ZXZ123.com. All rights reserved.
//

import UIKit

enum isPullDid {
    case UP
    case DOWN
}
// MARK: 基类的基本设置
class ZXZBaseViewController: UIViewController {


    /// 用户登录的标记
    lazy var isLogin:Bool = true;



    // 上啦还是下拉


    // 传递我现在访客速推的数据
    var inputVisitDic:[String:String]?

    /// b表格视图, 如果用户没有登录, 就不创建
    var tableView:UITableView?

    // 自定义UINav
    lazy var navBar:UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.cz_screenWidth(), height: 64))
    lazy var navItem:UINavigationItem = UINavigationItem()


    lazy var fpsLabel:YYFPSLabel = YYFPSLabel()


    /// 重写title的setter方法
    override var title: String?{
        didSet{
            navItem.title = title
            
        }
    }



    override func viewDidLoad() {
        super.viewDidLoad()
        setupBaseUI()
    }

    // 设置基本的视图
    private func setupBaseUI() {
        view.backgroundColor = UIColor.white
        automaticallyAdjustsScrollViewInsets = false

        setupNavView()

        isLogin ? setTableMainView() : setNoLoginMainView()
    }


    /// 加载数据
    func loadData(isPull:isPullDid) -> () {


    }



    // 导航视图
    private func setupNavView(){
        view.addSubview(navBar)

        navBar.items = [navItem]
        navBar.barTintColor = UIColor.white
        navBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.red]
        navBar.tintColor = UIColor.orange

        // 如果隐藏了nav的话 会自动隐藏20点 所以我们要关闭它
        automaticallyAdjustsScrollViewInsets = false
    }

    // 登录视图
    func setTableMainView(){

        tableView = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.cz_screenWidth(), height: UIScreen.cz_screenHeight()), style: UITableViewStyle.plain)
        tableView?.delegate = self
        tableView?.dataSource = self
        tableView?.contentInset = UIEdgeInsetsMake(navBar.height, 0, (tabBarController?.tabBar.height)!, 0)
        view.insertSubview(tableView!, belowSubview: navBar)

  


        fpsLabel.frame = CGRect(x: UIScreen.cz_screenWidth()-60, y: UIScreen.cz_screenHeight()-222, width: 60, height: 20)
        view.addSubview(fpsLabel)
        fpsLabel.backgroundColor = UIColor.red
    }

    // 未登录状态
    private func setNoLoginMainView(){

        let visitView = ZXZBaseVisitView(frame: view.bounds)
        view.addSubview(visitView)

        view.insertSubview(visitView, belowSubview: navBar)

        visitView.visitDic = inputVisitDic


        visitView.registerBtn.addTarget(self, action: #selector(ZXZBaseViewController.registerBtnClick), for: UIControlEvents.touchUpInside)

        visitView.loginBtn.addTarget(self, action: #selector(ZXZBaseViewController.loginBtnClick), for: UIControlEvents.touchUpInside)


        // 在nav上增加 注册和登录按钮

        navItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.plain, target: self, action: #selector(ZXZBaseViewController.registerBtnClick))

        navItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.plain, target: self, action: #selector(ZXZBaseViewController.loginBtnClick))
    }

    // 注册
    @objc private func registerBtnClick(){
        print("registerBtnClick");
    }

    // 登录
    @objc private func loginBtnClick(){
        print("loginBtnClick");
    }

}



// MARK: 实现数据源方法
extension ZXZBaseViewController:UITableViewDelegate, UITableViewDataSource{

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
}
